///////////////////////////////////////////////////////////////////////
//  CS 3715 (Winter 2012), Final Project                      		 //
//  Program File Name: PizzaPlaceServlet.java                     	 //
//       Student Name: Jonathan Hart-Wall, Kenny Hardiman            //
//         Login Name: jcwhw1, kch004                                //
//              MUN #: 200933455, 200930253    						 //
///////////////////////////////////////////////////////////////////////

package ca.FMP;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import ca.FMP.DBAccess;

/*
 * PizzaPlaceServlet Class, used to receive HTTPrequests from the index page, and sends XML to Banner page 
 */
public class PizzaPlaceServlet extends HttpServlet {

	/*
	 * @see
	 * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse) doGet method that receives
	 * HTTPRequests, and sends XML
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		/*
		 * Sets response type to xml. Sets PizzaPlaces to parameter of location.
		 */
		response.setContentType("text/xml");
		PrintWriter out = response.getWriter();
		String PizzaPlaces = request.getParameter("location");
		int count = 0;

		/**
		 * XML of doGet Method.It returns the Name and ImageLink of Pizza Places
		 * in location. If PizzaPlaces equals SJ, it will return all ImageLinks and Names
		 * in the database, else if will return all ImageLinks and Names of
		 * Pizza Places in that Area.
		 */
		out.println("<?xml version='1.0'?>");
		out.println("<Locations> \n");
		if (PizzaPlaces.equals("SJ")) {
			for (int i = 0; i < DBAccess.database.db.size(); i++) {
				out.println("<PizzaPlace> \n");
				out.println("<Name>" + DBAccess.database.db.get(i).getName()
						+ "</Name> \n");
				out.println("<ILink>"
						+ DBAccess.database.db.get(i).getImageLink()
						+ "</ILink> \n");
				out.println("</PizzaPlace> \n");
				count += 1;
			}
			out.println("<Count>" + count + "</Count>");
			out.println("</Locations>");

		} else {
			for (int i = 0; i < DBAccess.database.db.size(); i++) {
				if (DBAccess.database.db.get(i).getAreaOfTown()
						.equals(PizzaPlaces)) {
					out.println("<PizzaPlace> \n");
					out.println("<Name>"
							+ DBAccess.database.db.get(i).getName()
							+ "</Name> \n");
					out.println("<ILink>"
							+ DBAccess.database.db.get(i).getImageLink()
							+ "</ILink> \n");
					out.println("</PizzaPlace> \n");
					count += 1;
				}
			}
			out.println("<Count>" + count + "</Count>");
			out.println("</Locations>");
		}
	}
}